package com.example.transitionsdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import android.widget.TextView
import com.mjun.mtransition.*

class SecondActivity : AppCompatActivity() {

    private lateinit var viewGroup: ViewGroup

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
        val text = findViewById<TextView>(R.id.textView)
        viewGroup = findViewById(R.id.container)
        title = "SecondActivity"
        val name = MTransitionManager.getInstance().getTransition("example")?.bundle?.getString("name")
        text.text = name
        initTransition()
    }

    private fun initTransition() {
        MTransitionManager.getInstance().getTransition("example")?.apply {
            toPage().setContainer(viewGroup) { container ->
                val width = container.width
                fromPage().getTransitionView("container").translateX(0, -width /4)
                container.translateX(width, 0)
            }
            setOnTransitListener(object : TransitListenerAdapter() {
                override fun onTransitEnd(transition: MTransition?, reverse: Boolean) {
                    if (reverse) {
                        finish()
                        MTranstionUtil.removeActivityAnimation(this@SecondActivity)
                    }
                }
            })
            duration = 1000
            start()
        }
    }

    override fun onBackPressed() {
        MTransitionManager.getInstance().getTransition("example")?.apply {
            reverse()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        MTransitionManager.getInstance().destoryTransition("example")
    }
}